<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">店铺</label>
                    <div class="layui-input-block">
                        <select name="shop1" lay-search>
                            <option value="">请选择店铺</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售状态</label>
                    <div class="layui-input-block">
                        <select name="sales_state">
                            <option value=""></option>
                            <option value="1">新产品</option>
                            <option value="2">清仓品</option>
                            <option value="3">在售品</option>
                            <option value="4">下架品</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SKU等级</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku_grade" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button style="display:none" class="down" lay-submit="" lay-filter="LAY-list-down">下载</button>
                    <button type="reset" class="layui-btn layui-btn-primary reset" id="reset">重置</button>
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search">搜索</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-list"  lay-filter="LAY-list"></table>
            <!-- 行内工具 -->
            <script type="text/html" id="LAY-list-toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                {{# if(d.status==1){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="enable">禁用</a>
                {{# }else if(d.status==2){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable">启用</a>
                {{# } }}
                </script>
                <!-- 头部工具栏 -->
                <script type="text/html" id="toolbar">
                    <div class="layui-btn-group fl layui-btn-group-jy">
                        <a lay-event="add" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-add-circle-fine">&nbsp;添加</a>
                        <a lay-event="export" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle">&nbsp;导出</a>
                        <a  lay-event="batchAdd" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-add-circle-fine">&nbsp;批量编辑</a>
                    </div>
                    </script>


                </div>
            </div>
        </div>
        <script>
            layui.use(['admin', 'table', 'form', 'toolJ', 'upload'], function () {
                var $ = layui.$
                        , admin = layui.admin
                        , view = layui.view
                        , table = layui.table
                        , toolJ = layui.toolJ
                        , form = layui.form
                        , upload = layui.upload;
                //店铺sku管理 
                var shop = layui.data('yujiso').shop;//正常的sku状态
                var shopAll = layui.data('yujiso').shopAll;
                var selectHtml = '';var selectHtml1 = '';
                for (var i in shopAll) {
                    selectHtml += '<option value="' + shopAll[i] + '">' + shopAll[i] + '</option>';
                }
                for (var i in shop) {
                    selectHtml1 += '<option value="' + shop[i] + '">' + shop[i] + '</option>';
                }
                $("select[name='shop1']").append(selectHtml);
    
                form.render(null, 'LAY-list-form');
                table.render({
                    elem: '#LAY-list'
                    , url: '/index/Storesku/getList'
                    , toolbar: '#toolbar'
                    , defaultToolbar: ['filter']
                    , cols: [[
                            {type: 'checkbox'}
                            , {field: 'shop', title: '店铺'}
                            , {field: 'sku', title: 'SKU'}
                            , {field: 'cname', title: '中文名称', minWidth: 100}
                            , {field: 'sales_state_name', title: '销售状态'}
                            , {field: 'cycle_stage_name', title: '生命周期'}
                            , {field: 'sku_grade', title: 'SKU等级'}
                            , {field: 'state', title: '状态'}
                            , {field: 'operator', title: '修改人'}
                            , {field: 'update_time', title: '修改时间'}
                            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#LAY-list-toolbar'}
                        ]]
                    , page: true
                    , limit: 10
                    ,text:{none: '暂无相关数据!' }
 });
        //监听行内操作
        table.on('tool(LAY-list)', function(obj){
            var data = obj.data;
            data.type='edit';
            switch (obj.event) {
                case 'disable':
                    toolJ.ajax({"data":{"id":data.id,"status":1},"url":"/index/storesku/disableOrEnable"});
                    break;
                case 'enable':
                    toolJ.ajax({"data":{"id":data.id,"status":2},"url":"/index/storesku/disableOrEnable"});
                    break;
                case 'edit':
                    admin.popup({
                                title: '编辑'
                                , area: ['550px', '550px']
                                , id: 'LAY-popup-content-add'
                                , success: function (layero, index) {
                                    view(this.id).render('Storesku/edit', data).done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        //监听提交
                                        html=data.shop;
                                        $("input[name='shop']").val(html);
                                        form.render(null, 'layuiadmin-app-form-list');
                                        form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                            var field = data.field; //获取提交的字段
                                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                                            toolJ.ajax({"data": field, "url": "/index/Storesku/edit"});
                                            layui.table.reload('LAY-app-content-list'); //重载表格
                                            layer.close(index); //执行关闭 
                                            table.reload();
                                        });
                                    });
                                }
                            });
                    break;
            }
        });
        //监听头部工具栏 操作
        table.on('toolbar(LAY-list)', function (obj) {
		
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list')
                , data = checkStatus.data;
				//alert(JSON.stringify(data));
            //event对应模板的
            switch (obj.event) {
                case 'del':
                    layer.alert('开发中');
                    break;
                case 'export':
                    $('.down').click();
                    break;
                case 'add':
                admin.popup({
                        title: '添加'
                        ,area: ['550px', '550px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                          view(this.id).render('Storesku/edit',data).done(function(){
                            form.render(null, 'layuiadmin-app-form-list');
                            //监听提交
                            $("select[name='shop']").append(selectHtml1);
                                form.render(null, 'layuiadmin-app-form-list');
                                $('body').off('input propertychange').on('input propertychange', '#SKU', function () {
                                    skuVal = $("#SKU").val();
                                    $.post("index/produce/getSkuName", {sku: skuVal}, function (res) {
                                        if (Object.keys(res['data']).length > 0 && res['data'][0]['name'] != undefined) {
                                            skuName = res['data'][0]['name'];
                                        } else {
                                            skuName = '';
                                            layer.msg('sku不存在');
                                        }
                                        $('#cname').val(skuName);
                                    }, 'json');
                                });
                            form.on('submit(layuiadmin-app-form-submit)', function(data){
                              var field = data.field; //获取提交的字段
                              //
                               var index1='';
                              $.ajax({
                                    type: 'POST',
                                    url: '/index/Storesku/add',
                                    dataType: 'json',
                                    data: field,
                                    success: function (data) {
                                        layer.close(index1);
                                        if (data.code == 1) {
                                            if (data.msg) {
                                                layer.msg(data.msg, {"icon": 1});
                                            }
                                            layer.closeAll();
                                            $('.search').click();
                                        } else {
                                            layer.alert(data.msg, {"icon": 2});
                                            return false;
                                        }                                       
                                    },
                                    error: function () {
                                        layer.close(index1);
                                        layer.alert('系统繁忙,请稍后再试!', {"icon": 2});
                                        return false;
                                    },
                                    beforeSend: function () {
                                        index1=layer.load(2, {
                                            shade: [0.5, '#fff']
                                        });
                                    }
                                });
                              //提交 Ajax 成功后，关闭当前弹层并重载表格
                            });
                          });
                        }
                      });
                break;
            case 'batchAdd':
                //第一个链接为上传api 第二个链接是模板下载链接 第三个是成功回调
                toolJ.upload('/index/Storesku/batchOperator?batchType=add','/attachment/importcvs/storesku.csv',function (res) {
                    $('.search').click();
                });
               break;

            }
        });       
});
        </script>
